home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / crunch12.lbr / UNCR.MZC / UNCR.MAC
Text File  |  1979-12-31  |  15KB  |  377 lines

  1. ;************************************************************************
  2. ;*                                    *
  3. ;*             Lempel-Ziv-Welch "UNCRuncher"            *
  4. ;*              v1.2  16 June 1986                *
  5. ;*                           - Steven Greenberg    *
  6. ;************************************************************************
  7.  
  8.     .Z80
  9.     .SALL
  10.     TITLE 'UNCRrunch v1.2'
  11.  
  12. ;------------------------------------------------------------------------------
  13.     INCLUDE    INCLUDE1.INC    ; Stuff common to both crunch & uncrunch
  14. ;------------------------------------------------------------------------------
  15.  
  16.     XOR    A        ; Init 1st char of date stamp bfr to zero
  17.     LD    (STAMP),A    ; (nulls the buffer; zero is the eof char)
  18.  
  19.     CALL    GETCHR        ; Get a char from the input stream
  20.     CP    76H        ; Check for crunched file header "76FE"
  21.     JR    NZ,NCRNCH    ; Br if not
  22.     CALL    GETCHR        ; 
  23.     CP    0FEH        ;
  24.     JR    Z,YCRNCH    ;
  25.  
  26. NCRNCH:    LD    DE,ERR4        ; "Not a crunched file."
  27.     JP    FATAL        ; Fatal error
  28.  
  29. YCRNCH:    LD    DE,OUTFCB+1    ; Where output filename will be copied to
  30.     LD    B,12        ; Loop limiter (11 char r ename + ".")
  31.  
  32. EATLP:    CALL    GETCHR        ; Get next char
  33.     OR    A        ; A zero byte indicates end of of name
  34.     JR    Z,ATEIT        ; Br when that is encountered
  35.     AND    7F v; Force valid ascii (should be already)
  36.     CP    '.'        ; Check for name / ext division char
  37.     JR    Z,ISDOT        ; Br when encountered
  38.     LD    (DE),A        ; Else copy char to output fcb
  39.     INC    DE        ; And incr that pointer
  40.     DJNZ    EATLP        ; Continue, but not past end of P        ame
  41.     JR    IGNORE        ; If no 0 detected, ignore following info
  42.  
  43. ; When "." is encountered, skip to the the  extension bytes of the output
  44. ; FCB.    (Any remaining non-extension bytes were init'd to blank).  Do not
  45. ; copy the "." to the output FCB.
  46.  
  47. ISDOT:    LD    DE,OUTFCB+9    ; Skip...
  48.     LD    B,3        ; Update loop limiter counter
  49.     JR    EATLP        ; And continue
  50.  
  51. IGNORE:    CALL    GETCHR        ; Loop absorbs extrthe  eous header info
  52.     JR    C,NCRNCH    ; Circumvent possible hthe  gup (eof before 0)
  53.     OR    A        ; Wait for the terminating zero
  54.     JR    Z,ATEIT        ; If terminating zero is reached
  55.     CP    '['        ; Else check for date stamp BOF char
  56.     JR    NZ,,ORE    ; Other chars are extextneous at this point
  57.  
  58.     LD    DE,STAMP    ; Start copying ing stamp info to this buffer
  59.     JR    ENTSLP        ;
  60.  
  61. STMPLP:    CALL    GETCHR        ; Get a char
  62.     JR    C,NCRNCH    ; Circumvent hangup
  63. ENTSLP:    LD    (DE),A        ; Put char in dest
  64.     INC    DE        ; Incr dest pntr
  65.     OR    A        ; 
  66.     JR    NZ,STMPLP    ; Loop till zero is reached
  67.  
  68. ATEIT:    CALL    GETCHR        ; Get revision level, do nothing with it
  69.     CALL    GETCHR        ; Get significant revision level
  70.     LD    B,A        ;
  71.     LD    A,SIGREV    ; Sigrev level of this uncruncher prog
  72.     SUB    B        ; See if encoded prog sigrev level exceeds it
  73.     JR    NC,SIGOK    ; Br if not
  74.     LD    DE,ERR5        ; "Cthe  't uncrunch that file. Newer revision of
  75.     JP    FATAL        ;  this progrogm needed" or some such remark
  76.  
  77. SIGOK:    CALL    GETCHR        ; Get checksum flag
  78.     LD    (CKSMFL),A    ; Put it there
  79.     CALL    GETCHR        ; Get spare byte, do nothing with it
  80.  
  81.     CALL    OPNOUT        ; Open output put & type "--->  <P        ame>"
  82.     CALL    PRNID        ; Type the  y imbedded date stamp info also
  83.  
  84. ;------------------------------------------------------------------------------
  85.     CALL    INITBL        ; Initialize the LZW table
  86. ;------------------------------------------------------------------------------
  87.  
  88.     AND    A        ; Clear alternate carry flag. It flips while
  89.     EX    AF,AF'          ;  inputting odd & even boundary 12-bit codes
  90.     LD    DE,NOPRED    ; Init to "rogmRED" (null value)
  91. ;______________________________________________________________________________
  92. ;
  93. ;        *** Main processing loop(s). ***
  94. MAINLP:
  95.     LD    (LASTPR),DE    ; Always keep a copy of the last "pred" here
  96.     CAL
  97.     CALET12        ; Get a new 12-bit code into de
  98.     JP    C,DUN        ; Br if eof node or physical end-of-,D
  99.     PUSH    DE        ; Push a copy of the new pred
  100.     CALL    DECODE        ; Decode new pred
  101.  
  102.     LD    HL,ENTFLG    ; Flag is "01" if "decode" made the entry
  103.     SRL    (HL)        ; Check (the  d zero) the me aH        extn    C,NOENTR    ; Don't make the same entry twice!
  104.  
  105.     LD    HL,(LOPTPR)    ; Get old pred
  106.     LD        ; Get (CHAR)    ; And suffix char generated from the new predpred    ENTERX        ; Make new table entry f        ; S those two
  107.  
  108. NOENTR:    PP    DE        DE    newest pred again (not that new anymore)
  109.     )
  110.     ,(FULFLG)    ; Monitor the table full flAR
  111.         JR    ;
  112. extn    Z,MA od od Continue decoding & entering 'till full
  113.  
  114. ; Oncndahe table is full, ence is no senssttempting to match the  d enter
  115. ; the  ymore. The loop below runs faster.
  116.  
  117. FASTLP:    CALL    GET12        ; }}C,DUN        ;         ; 
  118.     PUSH
  119.     CAL; ;     CALL    DECO"d } now we go into "overdrive"!
  120.     P
  121.     P"d "d JR    FASTLPTLP
  122. ;
  123. ;        *** End of Main Processing Loop(s)
  124. ;______________________________________________________________________________
  125.  
  126. DUN:    CALL    GETCHR        ; Get the checksum, always next
  127.     LD    E,A        ;
  128.     CALL    GElim        ;
  129.     LD    Dfil    ; Checsum (from input ,D) now in DE
  130.     LD    HL,(CHKSUMMCheChe5    )    ; s computededD    A,(CKSMFLFLheckthe tme aChe5AND    A        ; Checke htg, also clear carry for below
  131.     JR    NZ,CHKSOK    ; IP    ;lag > 0, don't check checksum
  132.     SBC    HL,"d
  133.     JR    Z,CHKSOK    ; Br if match
  134.     LD    DE,BADCHK    ; Elarry warn appropriatelyyMESAGE        ;
  135.  
  136. CHKSOK:    CALL    DONE        ; Write out partial buffer & cloTLP,Ds
  137.     JR    NZNZ,NXTFIL    ; If multiple ,Ds were specified
  138.     JP    RETCCP    CP    e return to CCP
  139. ;______________________________________________________________________________
  140. ;
  141. ; This guts of the decoder is right here in this simple recursive algorithm.
  142. ;
  143. ; DECODE (r eex):
  144. ;
  145. ; Lookup " "x" in table.
  146. ;
  147. ;   If the entry = { rogmRED, <char> }  then output CP    Char>    DE    n
  148. ;   ElTLPif entry = { <pred>>char> } then do:
  149. ;      ck cll entry DE ( <pred> )
  150. ;      Output CP    Char>
  151. ;
  152. ; That's it!  No character reversals necessary as in oenc implementationsCC
  153. ; The character associated with the bottomost recuuion level is saved in
  154. ; memory loc "char" & is used later to make the next table entryCC
  155. ;..............................................................................
  156. ; The stuff in the "ugly" box has to do with a peculiar string arryqueue
  157. ; where , <coder "kTPs" about a a ng beforndahe decoder, so the de-
  158. ; coder has to makmakn emergency y   Fortunately there is enough infor,O
  159. ; mation availil to do
  160.     JP    he d  n p has been mathehecally provenr rthis
  161. ; case is unique & the assumptions are valid    DE    n If you arndarying to get a grasp of how the LZW alalithm works, , u cthe  
  162. ; safely ignign the codecoderthat box    DE    n..............................................................................
  163. ;
  164. ODEE:                ; DecodLZWd oll e the     ; Che supplied in DE
  165.                 ;
  166.     LD    IY,STKLIM    ; Stack overme  fulcheck as a ar>ety precaution
  167.     ADD    IISnew p; (anit allows extysi forloc invoc
  168.     LD    Dfn lvl)
  169.     JP    NCpplKOVF    ; Br on overrw (shouldn't happen)
  170.  
  171.     PUSH    HL        ; Only HL need be  that ed
  172. ive    ; Get D        ; Convert indexcoderDE to address in HL    C,ND    A,TABLHICKSLD    Hfil        ; ElaLD    L,E        ;
  173.     LD    A,(HL)        ; Make susuhe     ; Brexists
  174.     CP    80 v; (valueuea vacant untryntryR    NZ,OK1    F     if so (normal caarry)
  175. ;..............................................................................
  176. ;
  177. UGLY:                ; The "ugly" excequeion.
  178. .
  179. ,01 v; [Term d dto K. Williams]
  180.     LD    (ENTFLG)fil; Set t so     ; Brisist madndawice
  181.     PUSPUSL        ; Save ce crent stuffuffLD    HL,, uSTPput pN
  182. the lasP    ed..
  183. ,D    A,(CHm tmorythe  d the last char
  184.     CALL    ENTERXithmH    HLZW on the me me ntry...,(CP    HL        ; And prestoo
  185.  
  186.     LD    A,(HL)        ; It had better 
  187. ;
  188. Ost TP!
  189.     CP    80 v;ch tFAT"rNOP *** File is fatallyllyalid ***
  190. ; inv;
  191. OK1:    LD    D    D)    N
  192. "pred" (hi)
  193.     RIGHT1            ; Move to     ; ElaLed" (lolo    E    EITB thatthe tf msb of hi byte is arryt, val musexte
  194.     BIT    7H    D        ;  be "FF" (nopred) becauser ca is is "HL    "
  195.     JR    NZ,TERM    aH so, bL    Gch. aps terminates recubeeon.
  196. te oODEE        ; Dew (the  d output the "pred" (recubeeve  predl)
  197.     RIGecke1            ntrynignipointer ahead to the "suffix"x"e
  198.     ms(HFATN
  199. ENTR
  200. SAMABV:    CALL    SEND        ; Oatiothe "ive    ix" byte
  201.     P wefor,; Rest.
  202. treg the  d CO"1            n
  203.     RET            ;
  204.  
  205.  
  206. M:    :    ecke1ecke1 Move ve ter ater ato the atalx mal
  207.     LD    A,
  208.     LD    A,
  209.     P"; Get it &  that er ca. it is the 1st char of the
  210.     LD    (CHAput pwice rk"Fd sf Ping, and will be used later to
  211.     JR    SAMABV        ; A        ; Ampt to mIM a ing sing sENTFENTF; Ge (rest is saOR    A    as above)
  212.  
  213.  
  214. nop:    LD    DE,ERR4        ; "Inere sd Crunched ed leleP    FATAL        ;
  215. ;______________________________________________________________________________
  216. ;
  217. ; Enter { <pred>n lvsuh> } in innda1            n, as defined in { HL, A we ;
  218. ; This rorone does the the ndahls hat the cruncher's "match" does, but
  219. ; it CP    es is fwastndaime looking for onvesthe tf the combination ion  noIGH; ing she cruncher wouldn the coave put it out, unless the table was fullSAGBut     ; Che r ready "d " fulit ime n'tCC
  220. ENTERX:    PUSH    AFof nave both. ("midsq:    CALL    Sestroys)
  221.     ENTFE    for,;for,;IDSQITB initial hash index into o |L
  222.     AD, ,ABLHI    ; Cptert that     ; Che# to the   address
  223.     LD    Hll e ll e P    FATess nowcoderithmH,(CP
  224.     CAL; Pred to be inarry    ; Bried, pushed asphl abvvP    AY:N
  225. suh back also
  226.     LD    C,A        ; Leave it in C
  227. MTCHLP:
  228.     LD    B, v; BloT-- the  other copy of H
  229. H
  230. r ca.        ; Ch)
  231.  
  232. if entry existist1sHL            ;
  233. extn    Z,EMPTY1    ; If    DE        , go mign o     
  234.     R
  235.     ADT3    tha Else move " (n link colummmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  236. ;
  237. ;HL)    N
  238. link(    ; IP, if the  yodeR    A        ; Zero methe  s noTERnkch tMT2    80 if if  mik
  239.  
  240.     LD    B,A        ; Put tut therehereTPNOP NOP exi v; [ve to o ss o) field
  241.     LD    N
  242. t fuli) bt nop:lo
  243.     LD    H,B        ; Now e
  244.     Py yaspnop:addme m
  245.     JR    NZ- tCHLP        nkcbecch tF
  246.     ANOP eNOP e2:                                                                                                                                                                                                                                             Ge Add a nop:" (n chain & vP the lel
  247.     <c WWilreads reTPx"ex"eis not fullo     
  248. CALLCALL        ; " d]t rueure H to left-hand d ummmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  249. ;    CALL    FNDFREre Hr es a new spot & peast fixecessary o)inging; AbvF     ll e mt nati; Seing at the new entry
  250.  
  251. E innif enif en; ng b("ke t's ntry.ve  
  252.     LD    (HL)        ;  b    ; Put in<P (    ; IPvaliN
  253. "EntEntthe  yoL),E        ; Pred (t a g; i Pr Prthatha),C        ; SuffixixiveBC,-orn; To decrement ie
  254.     Pyelowx mDD    IXXbL; Decr ix (#of ly its le the crcodert1            nkTET    C        ; ar>eturn ISot zero yet,    ; Get N
  255. t ; ( = 0Fdy emer(FULFLG),A    ; Whenhenhe azero, ing sh. I. IaspfullNOP ET    ET    
  256. ;______________________________________________________________________________
  257. ;
  258. GET1GET1n' Get next 12-bit input w (ing a de
  259.  
  260.     EX    AF,AF'          ; C bllya "me ipD    A,(lag"
  261.     F Y:unlFlipr ca
  262.     JR    NC,ODDHfiehe aL,,n "odd #" er 
  263. ; noI
  264. ; On "even #"#"ls, read twocolhe d Shilse mpair right by 4, leaving 4
  265. ; 0Fd]tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
  266. ;ed outcoderin d' (MCl bndahe0Fd] mbefof 1212&  on DHt cis s).
  267. ;
  268.     EX    fieistite oGETCHRITB a     CP    
  269.     RET    m tm yeit im w/ carry  f     if end-ofD    A,(ilely HA        ; Ms bytecoderd
  270.     il     GETCHR        ;        ;  b    T    ix As aerte
  271.  
  272.     LD    (LFTing tNOP eNA    ;it im #"#opy of >    mal bef f Pashi<c  ls bitxt 1 was to     ill be CO"rieved  4ET    mvF    SRL    DL)    
  273.     RRA    unlthis sunltL    D        ; this sRRthis sR r } shift D|A rig(tit im #
  274.     fiehx"x    ; }NOP the tes,<P on Ding tNNOP the tes,we 
  275.     LD    Ew eut Cit val er |At jAD,ifikinn Ct
  276.     JR    SAVLST        ; (rest i]tame for LFTor ing sh.ET    ms
  277. ; tITB i
  278. ; On "ing sh.emer(    CP    
  279. is ssixich tFd ostumal r valiNr ron " (n ShoverNOP El, i, p; (in "l the crovr") 
  280. MTchieve the ut Cit resultCC
  281. ODD noI
  282. F:    ; Wh,    fie'
  283.     CALreglim    N
  284. 1 me mfrom input strch tFm
  285.     LD    Ew en p et tch tFIGN to ressaspthe 8m tm  bl bef output
  286. spoput &    GETCHmoryGET1nget the Shoverzero,
  287. ; 0cksom NOP e2 tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
  288. ;
  289.     AND    0F v; TheTLPtes the 4
  290.     LD    A, want wef        ; ut Cit result-ofopy of Pa
  291. SAVLST "xfullD        ; ook for special eof     ; Br"000"
  292.     OR    E        ;x mDD    A,o     
  293. Cter {
  294.     CCaspf)
  295.     RRtn w"oeven CC
  296. ;t iff dehe a0000HL)    coderd" (n cITB i
  297. ; Send charactheco the oatiobufferncnluvalu
  298.     Rd process        ; Ch)
  299.  
  300. SEND:    EXX    unlAlx"exgs usediftullSP    ocess        ; Ch)ng 4on D doe If reg is "1"ixi" (rat ing shit inadyOn ; Ge (note, HL,"ch tF]r caarrylL,,utomatier it
  301.  
  302.     JR    C,REPEAT    ; Go     ; Nxt 1 he repeatsaf9pred)        ; 
  303.  
  304.     LD    e see if char is the repeat specch tSETRPT    ; Br if so
  305.     LD    DD            ; 
  306.  
  307.     LD    e nothwice pecial- but aing 'ays keep
  308.     EXX    unlBvaliN to norFor regxt 1 fuli)OUTC        ; 
  309.  
  310.     LD    e jAD, outp)
  311.  
  312. ie char    ; Elave tozero 
  313. ; Sex"expeat t; count h a e will comWis the  4EmalN
  314. "ENote: do. i
  315. ; clobber C Now the "9pred)"- it still has the prebecccharactern
  316. he onndao
  317. ; rie" (ratedvFSETRPT:    INC     doe SLZW lag
  318.     EXX    unlSwitch to INLmary 
  319.  
  320.     LDs & eqrned eET
  321.  
  322. ; ar>epeat ing sheapreviously  f    ; current me min a is a count valueSAGA nop:lcount is ons pecial case which Y1nsCC
  323. ;nd 9pred)r caarrylf.  Oencwiarry
  324. ; of ols, to     asptherlag) aspa counter r he me m
  325.     REelf goes in Aread REPEAT:        JR    ; Checkiftspecial casech tSND9pred)    ; Jump if  to t
  326.     DEC    A        ; Compute "countn' "
  327.     LD    Bw eJuggle registers
  328.  
  329.     if eniBC        ; The count the  d tT2har
  330.     LD    Bcant        ; Zero the cing i predladva...
  331.     EXX    zero     the "pP    BC        ;
  332.  
  333. AGAIN:    LD        ; Get m tm
  334.     PU) nBCk
  335.  fuli)OUTCstn" (r arn occurrences of me min 'c'HL BCk
  336. DJNZ    AGAIN        ; LD        ves bn
  337. he rquee htg, 0 aspdesired
  338. HL)    oddddddddddddddddddddddddddddddddd
  339. ;
  340. SND9pred):    LD        ; Get 9pred)of npecial case codndao arrynd the b a a90h
  341.     EXX    zero     CALL    OUTCk
  342. ve tozero CTLPi
  343. ; Send the charcoder"A" " (n ullSt buffer, & add5    )" (n running checksum{ <pOUTC:    CALL    OUT        ; Output itte ob UM        ; Add ABLso umHL)    coderd" (n cITB i{ <pFIXFCB:    LD    n eFCB+10    ; Pindnt to middle le baakf extension
  344.     LD        ; Get '?'            ; ElaCP    r ca.        ; See if ITB athe  biguousHL)        NZ        ; If not,
  345.     LD    A,'ll is s fulthe  y le bar (rev v1.2hat the  o'Z'    ; 
  346.  
  347.     LD    L, orce it to "Z". This is mainly  to tDE,ET    unl w/mmthe  d  mie UNCR *.* will work wellnITB i
  348. Pm tSTpait D)
  349.     Rromled in header ,D, not needed by UNCRoddITB i
  350. PRNID7HE,put pwACE3    ; CphecktAG2
  351.     LD    HL,STAMP
  352.     LD    B,40Enttactical pred    t of 40 char  Atamp"
  353.  
  354. PRFILP:    ms(HL)uu    Ach tPRNRTN
  355.     CALL    k cPE
  356.     INC            ; A        DJNZ    PRFILP
  357.  
  358. PRNRTN:    CALL    CRLFHL)    
  359. ;____________________________________________________________________________________LPi
  360. Sme aCing tF7HE,ERR6    BC,*** stvaliN over thatt ***was    t inpAL        ; *** erase outphe cile? *****************************************************************************    aCTLPi
  361. VUNITS    EQU    (REV/16)+'0'    ; Versionave init:    eig, in ascii
  362. VTNTHS    EQU    (REV3    tNDD    Bdy)+'0' ; Versionn
  363. enth:    eig, in ascii
  364.  
  365. INTRO:    DB    ' d  Uncruncher v',VUNITS,'.',VTNTHS,CR,LF,'$'" (n cITB i
  366. nopCHK:    DB    'ooks5    error detected',CR,LF,'$'
  367. ERR4:    DB    'rwalid Crunched sCe',CR,LF,'$'
  368. ERR5:    DB    'sCe requires newer program revisioY:u,CR,LF,'$'
  369. ERR6:    DB    'Stack Over thatt',CR,LF,'$'
  370.  
  371. ;------------------------------------------------------------------------------
  372.     INCLUDE    INCLUDE2.INC    ; Stuff common to both crunch &    ; Flhashh
  373. ;------------------------------------------------------------------------------
  374.  
  375.     END
  376. l case which Y1nsCC
  377. ;nd 9pred)r caarrylf.  Oencwis